/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: SysOssController
 * Author:   zhouheng
 * Date:     2018/7/13 10:46
 * Description: oss控制类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.modular.oss.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.hengby.hengcrud.core.common.factory.PageFactory;
import com.hengby.hengcrud.core.exception.HengCrudException;
import com.hengby.hengcrud.core.exception.HengCrudExceptionEnum;
import com.hengby.hengcrud.core.page.PageBT;
import com.hengby.hengcrud.modular.base.controller.BaseController;
import com.hengby.hengcrud.modular.base.tips.SuccessTips;
import com.hengby.hengcrud.modular.base.tips.Tips;
import com.hengby.hengcrud.modular.oss.cloud.OSSFactory;
import com.hengby.hengcrud.modular.oss.model.SysOss;
import com.hengby.hengcrud.modular.oss.service.OssService;
import com.hengby.hengcrud.modular.sys.warpper.LogWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈oss控制类〉
 *
 * @author zhouheng
 * @create 2018/7/13
 * @since 1.0.0
 */
@RestController
@RequestMapping("/sys/oss")
public class SysOssController extends BaseController {

    @Autowired
    private OssService ossService;
//    @Autowired
//    private SysConfigService sysConfigService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("文件列表")
//    @RequiresPermissions("sys:oss:all")
    public Object list(@RequestParam(value = "pageBT") PageBT pageBT){
        Page<SysOss> page = new PageFactory<SysOss>().defaultPage();
//
        List<Map<String, Object>> result = ossService.getSysOssList(page, page.getOrderByField(), page.isAsc());

//        page.setRecords((List<SysOss>) new LogWarpper(result).wrap());

//        return super.packForBT(page);

        PageHelper.setPagination(page);
        page.setRecords((List<SysOss>) new LogWarpper(result).wrap());
        page.setTotal(PageHelper.freeTotal());//获取总数并释放资源 也可以 PageHelper.getTotal()

        return super.packForBT(page);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Tips upload(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            throw new HengCrudException(HengCrudExceptionEnum.FILE_IS_EMPTY);
        }

        //上传文件
        String suffix = file.getOriginalFilename().
                substring(file.getOriginalFilename().lastIndexOf("."));

        try {
            String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
            //保存文件信息
            SysOss ossEntity = new SysOss();
            ossEntity.setUrl(url);
            ossEntity.setCreateDate(new Date());
            ossService.insert(ossEntity);
        }catch (IOException e){
            throw new HengCrudException(HengCrudExceptionEnum.FILE_READ_ERROR);
        }

        return new SuccessTips();
    }

}